﻿package  
{
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2DebugDraw;
	import Box2D.Dynamics.b2World;
	import flash.display.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author ...
	 */
	public class main extends MovieClip
	{

		public static var m_physScale:Number = 30;
		public var m_screen:MovieClip = new MovieClip();
		public var m_cam:camera = new camera();
		public var m_vehicle:vehicle = undefined;
		public var m_terrain:terrain;
		public var m_gaHandler:geneticHandler;
		public var m_collisionHandler:collisionHandler;
		public var m_world:b2World;
		public var m_debugDrawOn:Boolean = false;
		public var m_console:console;
		
		public function main() 
		{
			setupWorld();
			setupScreen(m_debugDrawOn);
			setupTerrain();
			
			m_console = new console(m_screen);
			m_gaHandler = new geneticHandler(this);
			m_collisionHandler = new collisionHandler(this);
			m_world.SetContactListener(m_collisionHandler);	
			
			//setupVehicle(); 
			//setupDebugVehicle();	
			
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, on_mouse_wheel);	
			stage.addEventListener(Event.ENTER_FRAME, update);
		}
		
		private function update(e:Event):void 
		{
			m_world.Step(1.0 / 30.0, 10);			
			
			m_gaHandler.update();
			m_vehicle.update();
			m_cam.update(m_vehicle);
			
			if (!m_debugDrawOn)
			{
				m_terrain.draw(m_screen, m_cam);
				m_vehicle.draw(m_screen, m_cam);
			}
		}
		
		public function setupScreen(debugOn:Boolean):void
		{
			addChild(m_screen);
			
			if (debugOn)
			{	
				var debugDraw:b2DebugDraw = new b2DebugDraw();
				debugDraw.m_drawScale = 18;
				debugDraw.m_sprite = m_screen;
				debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
				debugDraw.m_lineThickness = 2.0;
				debugDraw.m_fillAlpha = 0.6;
				m_world.SetDebugDraw(debugDraw);
			}
		}
		
		
		private function setupWorld():void
		{
			var worldSize:b2AABB = new b2AABB();
			worldSize.lowerBound.Set( -5000 / m_physScale, -5000 / m_physScale);
			worldSize.upperBound.Set(5000 / m_physScale, 5000 / m_physScale);
			var gravity:b2Vec2 = new b2Vec2(0, 25);
			var ignoreSleeping:Boolean = true;
			
			m_world = new b2World(worldSize, gravity, ignoreSleeping);
			m_world.SetContinuousPhysics(false);
		}
		
		private function setupTerrain():void
		{
			var x:int = 250;
			var y:int = 120;
			var rand:int;
			
			var vertices:Array = new Array();
			vertices.push(new cartesian( -500, 120));	
			vertices.push(new cartesian(250, 120));
			while (x < 3000)
			{
				x += geneticHandler.randRange(20, 100);
				y += geneticHandler.randRange( -50, 50);
				vertices.push(new cartesian(x,y));	
			}
			vertices.push(new cartesian(3100, 120));
			vertices.push(new cartesian(3500, 120));
			
			m_terrain = new terrain(vertices, m_world);
		}
		
		private function setupVehicle():void
		{
			m_vehicle = new vehicle(new dna(), m_world);
		}
		
		private function setupDebugVehicle():void
		{
			// Setup Vehicle
			var car_dna:dna = new dna();
			car_dna.m_px[0] = 0;
			car_dna.m_py[0] = 100;
			car_dna.m_radius[0] = 25;
			
			car_dna.m_px[1] = 100;
			car_dna.m_py[1] = 100;
			car_dna.m_radius[1] = 25;
			
			car_dna.m_px[2] = 50;
			car_dna.m_py[2] = 50;
			car_dna.m_radius[2] = 10;
			
			car_dna.m_px[3] = 40;
			car_dna.m_py[3] = 50;
			car_dna.m_radius[3] = 13;
			m_vehicle = new vehicle(car_dna, m_world);
		}
		
		public function restart():void
		{
			if (m_vehicle != undefined)
			{
				m_screen.removeChild(m_vehicle.m_sprite);
				m_vehicle.kill();

			}
			
		}
		
		private function on_mouse_wheel(e:MouseEvent):void 
		{
			m_cam.adjustZoom(e.delta / 12);	
		}
		
	}
	
}